import sys
import pygame
import time

sys.path = [".."] + sys.path

from UAITest import UAITest
from Movable import Movable
from Box     import Box

import UAI
import Main
import Debug

class TestPathFinding(UAITest):
    def setUp(self):
        UAITest.setUp(self)
        UAI.game.items.append(Box((100,0  ),( 20,160)))
        UAI.game.items.append(Box((240,100),( 20,160)))
        UAI.game.items.append(Box(( 80,140),(160, 20)))
        UAI.game.items.append(Box((380,160),( 40,200)))
        UAI.game.items.append(Box((180,320),(160, 20)))
        UAI.game.items.append(Box((340, 60),(160, 20)))
        UAI.game.items.append(Box((80 ,400),( 40, 20)))
        UAI.game.items.append(Box((160,400),(140, 20)))
        UAI.game.items.append(Box((320,460),( 20, 20)))
        UAI.game.items.append(Box((480,320),( 20,160)))
        UAI.game.items.append(Box((480,120),(140, 40)))
        UAI.game.items.append(Box((420,240),(140, 20)))
        UAI.pathfinding.preMapGeneration()
        
        
    def tearDown(self):
        while UAI.game.items:
            UAI.game.items.pop()
    
    def pathfind(self, startpos, endpos):
        starttime = time.time()
        node = UAI.pathfinding.findPath(startpos, endpos)
        searchtime = time.time() - starttime
        Debug.log('(' + str(round(searchtime, 2)) + 's', False)
        testMovable = Movable((0,0))
        node = testMovable.smoothpath(node)
        smoothtime = time.time() - starttime - searchtime
        Debug.log(str(round(smoothtime, 2)) + 's ' + str(round(smoothtime + searchtime, 2)) +  's)')
    
    def path01(self):
        for i in range(0,20):
            self.pathfind(( 20,200), (600,460))
            self.pathfind((600,460), (140, 20))
            self.pathfind((140, 20), ( 80, 20))
